<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf8">
    <!--

    @license twgl.js Copyright (c) 2015, Gregg Tavares All Rights Reserved.
    Available via the MIT license.
    see: http://github.com/greggman/twgl.js for details

    -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <meta property="og:title" content="TWGL.js - picking" />
    <meta property="og:type" content="website" />
    <meta property="og:image" content="http://twgljs.org/examples/screenshots/picking.png" />
    <meta property="og:description" content="TWGL.js - picking" />
    <meta property="og:url" content="http://twgljs.org" />

    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:site" content="@greggman">
    <meta name="twitter:creator" content="@greggman">
    <meta name="twitter:domain" content="twgljs.org">
    <meta name="twitter:title" content="TWGL.js - picking">
    <meta name="twitter:url" content="http://twgljs.org/examples/picking.html">
    <meta name="twitter:description" content="TWGL.js - picking">
    <meta name="twitter:image:src" content="http://twgljs.org/examples/screenshots/picking.png">

    <link href="/resources/images/twgljs-icon.png" rel="shortcut icon" type="image/png">

    <title>twgl.js - picking (bad)</title>
    <style>
      *, *:before, *:after {
          -webkit-user-select: none;
          -moz-user-select: none;
          -ms-user-select: none;
          user-select: none;
      }
      body {
          margin: 0;
          font-family: monospace;
      }
      canvas {
          display: block;
          width: 100vw;
          height: 100vh;
      }
      #b {
        position: absolute;
        top: 10px;
        width: 100%;
        text-align: center;
        z-index: 2;
      }
    </style>
  </head>
  <body>
    <canvas id="c"></canvas>
    <div id="b"><a href="http://twgljs.org">twgl.js</a> - picking (bad)</div>
  </body>
  <script id="vs" type="notjs">
uniform mat4 u_worldViewProjection;
uniform vec3 u_lightWorldPos;
uniform mat4 u_world;
uniform mat4 u_viewInverse;
uniform mat4 u_worldInverseTranspose;

attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texcoord;

varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;

void main() {
  v_texCoord = a_texcoord;
  v_position = (u_worldViewProjection * a_position);
  v_normal = (u_worldInverseTranspose * vec4(a_normal, 0)).xyz;
  v_surfaceToLight = u_lightWorldPos - (u_world * a_position).xyz;
  v_surfaceToView = (u_viewInverse[3] - (u_world * a_position)).xyz;
  gl_Position = v_position;
}
  </script>
  <script id="fs" type="notjs">
precision mediump float;

varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;

uniform vec4 u_lightColor;
uniform vec4 u_diffuseMult;
uniform vec4 u_flashColor;
uniform sampler2D u_diffuse;
uniform vec4 u_specular;
uniform float u_shininess;
uniform float u_specularFactor;

vec4 lit(float l ,float h, float m) {
  return vec4(1.0,
              abs(l),//max(l, 0.0),
              (l > 0.0) ? pow(max(0.0, h), m) : 0.0,
              1.0);
}

void main() {
  vec4 diffuseColor = texture2D(u_diffuse, v_texCoord) * u_diffuseMult;
  vec3 a_normal = normalize(v_normal);
  vec3 surfaceToLight = normalize(v_surfaceToLight);
  vec3 surfaceToView = normalize(v_surfaceToView);
  vec3 halfVector = normalize(surfaceToLight + surfaceToView);
  vec4 litR = lit(dot(a_normal, surfaceToLight),
                    dot(a_normal, halfVector), u_shininess);
  vec4 outColor = vec4((
  u_lightColor * (diffuseColor * litR.y +
                u_specular * litR.z * u_specularFactor)).rgb,
      diffuseColor.a);
  gl_FragColor = outColor + u_flashColor;
}
  </script>
  <script id="picking-vs" type="notjs">
uniform mat4 u_worldViewProjection;

attribute vec4 a_position;
attribute vec2 a_texcoord;

varying vec2 v_texCoord;

void main() {
  v_texCoord = a_texcoord;
  gl_Position = u_worldViewProjection * a_position;
}
  </script>
  <script id="picking-fs" type="notjs">
precision mediump float;

varying vec2 v_texCoord;

uniform float u_threshhold;
uniform vec4 u_pickColor;
uniform vec4 u_diffuseMult;
uniform sampler2D u_diffuse;

void main() {
  vec4 diffuseColor = texture2D(u_diffuse, v_texCoord) * u_diffuseMult;
  if (diffuseColor.a <= u_threshhold) {
    discard;
  }
  gl_FragColor = u_pickColor;
}
  </script>
  <script src="../dist/4.x/twgl-full.min.js"></script>
  <script src="../3rdparty/chroma.min.js"></script>
  <script>
    "use strict";
    twgl.setDefaults({attribPrefix: "a_"});
    const m4 = twgl.m4;
    const gl = document.querySelector("#c").getContext("webgl");
    const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
    const pickingProgramInfo = twgl.createProgramInfo(gl, ["picking-vs", "picking-fs"]);

    const shapes = [
      twgl.primitives.createCubeBufferInfo(gl, 2),
      twgl.primitives.createSphereBufferInfo(gl, 1, 24, 12),
      twgl.primitives.createPlaneBufferInfo(gl, 2, 2),
      twgl.primitives.createTruncatedConeBufferInfo(gl, 1, 0, 2, 24, 1),
      twgl.primitives.createCresentBufferInfo(gl, 1, 1, 0.5, 0.1, 24),
      twgl.primitives.createCylinderBufferInfo(gl, 1, 2, 24, 2),
      twgl.primitives.createDiscBufferInfo(gl, 1, 24),
      twgl.primitives.createTorusBufferInfo(gl, 1, 0.4, 24, 12),
    ];

    function rand(min, max) {
      return min + Math.random() * (max - min);
    }

    let pickId = -1;

    // Shared values
    const lightWorldPosition = [1, 8, -10];
    const lightColor = [1, 1, 1, 1];
    const camera = m4.identity();
    const view = m4.identity();
    const viewProjection = m4.identity();

    const tex = twgl.createTexture(gl, {
      min: gl.NEAREST,
      mag: gl.NEAREST,
      src: [
        255, 255, 255, 255,
        192, 192, 192, 255,
        192, 192, 192, 255,
        255, 255, 255, 255,
      ],
    });

    function makePickingColor(id) {
      return [
        ((id >>  0) & 0xFF) / 0xFF,
        ((id >>  8) & 0xFF) / 0xFF,
        ((id >> 16) & 0xFF) / 0xFF,
        ((id >> 24) & 0xFF) / 0xFF,
      ];
    }

    const objects = [];
    const drawObjects = [];
    const pickDrawObjects = [];
    const numObjects = 100;
    const baseHue = rand(0, 360);
    for (let ii = 0; ii < numObjects; ++ii) {
      const uniforms = {
        u_lightWorldPos: lightWorldPosition,
        u_lightColor: lightColor,
        u_diffuseMult: chroma.hsv((baseHue + rand(0, 60)) % 360, 0.4, 0.8).gl(),
        u_flashColor: [0, 0, 0, 0],
        u_specular: [1, 1, 1, 1],
        u_shininess: 50,
        u_specularFactor: 1,
        u_diffuse: tex,
        u_viewInverse: camera,
        u_world: m4.identity(),
        u_worldInverseTranspose: m4.identity(),
        u_worldViewProjection: m4.identity(),
        u_threshhold: 0.1,
        u_pickColor: makePickingColor(ii),
      };
      drawObjects.push({
        programInfo: programInfo,
        bufferInfo: shapes[ii % shapes.length],
        uniforms: uniforms,
      });
      pickDrawObjects.push({
        programInfo: pickingProgramInfo,
        bufferInfo: shapes[ii % shapes.length],
        uniforms: uniforms,
      });
      objects.push({
        translation: [rand(-10, 10), rand(-10, 10), rand(-10, 10)],
        ySpeed: rand(0.1, 0.3),
        zSpeed: rand(0.1, 0.3),
        uniforms: uniforms,
      });
    }

    const pickingFBI = twgl.createFramebufferInfo(gl);

    // NOTE: !!
    //
    // Picking doesn't really make sense in TWGL. Picking requires structure
    // There must be some kind of database or scene graph of things to pick.
    // TWGL doesn't impose any kind of database or scene graph because it's
    // just a set of helpers for raw WebGL. Any structure on top of TWGL is
    // your structure.
    //
    // It also requires some kind of material stucture so that all objects
    // can be rendered with both whatever shader(s) render them normally
    // AND a shader for picking.
    //
    // THREE.js on the other hand has a scene graph that contains objects
    // so it makes sense to be able to pick one of those objects. It also
    // contains various materials and shader generators and imposes a
    // standard structure on those, what their inputs are etc.
    //
    // The code in this exampele was just the quickest thing I could think
    // of to make picking example. It's the primitives.html example
    //
    // It works by
    //
    // 1. Making the picking shader compatible with the phong shader
    //
    // 2. Making the 2 shaders use the same uniforms
    //
    //    Since they use the same uniforms then when drawing the picking
    //    there are no matricies to set because we already set them all
    //    last time things were drawn.
    //
    // 3. Adding a u_flashColor uniform to the phong shader
    //
    // 4. Having 2 parallel drawObjectList lists. One for drawing, one for picking
    //
    // Effectively it's a big hack that has nothing to do with the structure of
    // a program that would actually be used to pick something
    //
    // On the other hand what it does show is
    //
    // 1. using `twgl.createFrameBufferInfo` to create an RGBA + Depth buffer
    //    the same size as the canvas which is the default with no arguments
    //
    // 2. using `twgl.resizeFramebufferInfo` to match the new size of the canvas
    //    again the default with no extra arguments
    //
    // 3. indirectly the fact that `twgl.bindFramebufferInfo` sets the viewport
    //
    // 4. examples of getting a relative mouse and/or touch position
    //
    //    NOTE: it assumes the canvas has no border, padding, margin.

    function pickObject(inputX, inputY) {
      // If something is picked restore its color
      if (pickId >= 0 && pickId < objects.length) {
        const object = objects[pickId];
        object.uniforms.u_flashColor = [0, 0, 0, 0];
      }

      const rect = gl.canvas.getBoundingClientRect();
      const w = gl.canvas.clientWidth;
      const h = gl.canvas.clientHeight;
      const x = (inputX - rect.left) * gl.canvas.width  / w | 0;
      const y = gl.canvas.height - ((inputY - rect.top ) * gl.canvas.height / h | 0) - 1;

      // resize pickingFBI to match canvas
      twgl.resizeFramebufferInfo(gl, pickingFBI);
      twgl.bindFramebufferInfo(gl, pickingFBI);

      gl.clearColor(1, 1, 1, 1);
      gl.enable(gl.DEPTH_TEST);
      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
      twgl.drawObjectList(gl, pickDrawObjects);

      const pickColor = new Uint8Array(4);
      gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pickColor);
      pickId = (pickColor[0] <<  0) +
               (pickColor[1] <<  8) +
               (pickColor[2] << 16) +
               (pickColor[3] << 24);
    }

    gl.canvas.addEventListener('click', function(e) {
      pickObject(e.clientX, e.clientY);
    });
    gl.canvas.addEventListener('touchstart', function(e) {
      e.preventDefault();
      pickObject(e.touches[0].clientX, e.touches[0].clientY);
    });
    gl.canvas.addEventListener('contextmenu', function(e) {
      e.preventDefault();
      return false;
    });

    function render(time) {
      time *= 0.001;
      twgl.resizeCanvasToDisplaySize(gl.canvas);
      twgl.bindFramebufferInfo(gl, null);

      gl.enable(gl.DEPTH_TEST);

      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

      const projection = m4.perspective(30 * Math.PI / 180, gl.canvas.clientWidth / gl.canvas.clientHeight, 0.5, 100);
      const eye = [1, 4, -20];
      const target = [0, 0, 0];
      const up = [0, 1, 0];

      m4.lookAt(eye, target, up, camera);
      m4.inverse(camera, view);
      m4.multiply(projection, view, viewProjection);

      objects.forEach(function(obj) {
        const uni = obj.uniforms;
        const world = uni.u_world;
        m4.identity(world);
        m4.rotateY(world, time * obj.ySpeed, world);
        m4.rotateZ(world, time * obj.zSpeed, world);
        m4.translate(world, obj.translation, world);
        m4.rotateX(world, time, world);
        m4.transpose(m4.inverse(world, uni.u_worldInverseTranspose), uni.u_worldInverseTranspose);
        m4.multiply(viewProjection, uni.u_world, uni.u_worldViewProjection);
      });

      // If something is picked flash its color
      if (pickId >= 0 && pickId < objects.length) {
        const object = objects[pickId];
        object.uniforms.u_flashColor = ((time * 20 | 0) % 2) ? [0.2, 0.2, 0.2, 0.2] : [1, 0, 0, 1];
      }

      twgl.drawObjectList(gl, drawObjects);

      requestAnimationFrame(render);
    }
    requestAnimationFrame(render);

  </script>
</html>
